#pragma once

#ifndef __OGREWIDGET__INCLUDED__HH__
#define __OGREWIDGET__INCLUDED__HH__
 
#include <Ogre.h>
#include <QtGui>
 
#ifdef Q_WS_X11
  #include <QtGui/QX11Info>
#endif

#include <LitCameraController.h>
#include "MouseDrag.h"
class OgreWidget;
class OgreWidgetListener : public Ogre::FrameListener
{
protected:
	OgreWidget* mWidget;
public:
	OgreWidgetListener( OgreWidget * widget = NULL );
	virtual ~OgreWidgetListener();
	
	void setWidget( OgreWidget* w );
	OgreWidget* widget() const;

	virtual void mouseMoved( const QPoint& pos, Qt::MouseButtons );
	virtual void mousePressed( const QPoint& pos, Qt::MouseButtons );
	virtual void mouseReleased( const QPoint& pos, Qt::MouseButtons );
	virtual void mouseDragged(  MouseDrag & drag  );
	virtual void mouseDragging(  MouseDrag & drag  );

	virtual void createScene( );
	virtual void destroyScene( );
	virtual bool frameRenderingQueued( const Ogre::FrameEvent& evt );
};

class OgreWidget : public QWidget, public Ogre::LogListener
{
    Q_OBJECT
	std::vector<OgreWidgetListener*> mListeners;
public:
    OgreWidget(QWidget *parent = NULL, QPlainTextEdit* log_edit = NULL );
    virtual ~OgreWidget();
	
	void addListener( OgreWidgetListener * lst ){ mListeners.push_back( lst ); }
	void removeListener( OgreWidgetListener * lst ){ for( auto i=mListeners.begin(); i!=mListeners.end(); i++ ) if( (*i) && (*i) == lst ){ mListeners.erase(i); return; } }
 
	Ogre::Root         *ogreRoot() const{ return mogreRoot; }
    Ogre::SceneManager *ogreSceneManager() const{ return mogreSceneManager; }
    Ogre::RenderWindow *ogreRenderWindow() const{ return mogreRenderWindow; }
    Ogre::Viewport     *ogreViewport() const{ return mogreViewport; }
    Ogre::Camera       *ogreCamera() const{ return mogreCamera; }
	MouseDrag			mouseDrag() const{ return mDrag; }
public slots:
    void setBackgroundColor(QColor c);
signals:
	void ogreInitialise( );
	void ogreShutdown();

protected:
	void mousePressEvent( QMouseEvent*evt );
	void mouseReleaseEvent( QMouseEvent*evt );
	void mouseMoveEvent( QMouseEvent*evt );
	void mouseDragging( MouseDrag & drag );
	void mouseDragged( MouseDrag & drag );
    virtual void moveEvent(QMoveEvent *e);
    virtual QPaintEngine* paintEngine() const;
    virtual void paintEvent(QPaintEvent *e);
    virtual void resizeEvent(QResizeEvent *e);
    virtual void showEvent(QShowEvent *e);
 
	void messageLogged( const Ogre::String& message, Ogre::LogMessageLevel lml, bool maskDebug, const Ogre::String &log_name );

private:
    void initOgreSystem();
 
private:
	MouseDrag				mDrag;
	Lit::CameraController* mCamMgr;
    Ogre::Root         *mogreRoot;
    Ogre::SceneManager *mogreSceneManager;
    Ogre::RenderWindow *mogreRenderWindow;
    Ogre::Viewport     *mogreViewport;
    Ogre::Camera       *mogreCamera;
 
};
 
#endif // OGREWIDGET_H